﻿#include <QFile>
#include "CCorrectResult.h"
#include "CGlobal.h"
#include "CImgShowDlg.h"
#include "CSqlOper.h"
#include "CVideoResultQuery.h"
#include "Public.h"

CCorrectResult::CCorrectResult(QString sPointGuid, QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowTitle(A2T("跑偏结果查询"));
	// 获取 Qt 窗口的原生 HWND 句柄
	HWND hwnd = reinterpret_cast<HWND>(this->winId());
	setWindowsTitleBarColor(hwnd, RGB(56, 63, 81));
	m_sPointGuid = sPointGuid;
	m_nPerPageCount = 100;//默认每页显示100条数据
	m_nCurrentPage = 1;//当前页数第一页
	m_nTotalCount = 0;//记录总数
	m_nPageCount = 0;//总页数
	setStyleSheetFile();
	initConnect();
	initData();
}

CCorrectResult::~CCorrectResult()
{}

void CCorrectResult::setStyleSheetFile()
{
	//设置对话框标题
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void CCorrectResult::initConnect()
{
	connect(ui.pushButtonQuery, &QPushButton::clicked, this, &CCorrectResult::slotPushButtonQuery);
	connect(ui.pushButtonResult, &QPushButton::clicked, this, &CCorrectResult::slotPushButtonResult);
	connect(ui.pushButtonDelDay, &QPushButton::clicked, this, &CCorrectResult::slotDelDay);
	connect(ui.pushButtonAddDay, &QPushButton::clicked, this, &CCorrectResult::slotAddDay);
	connect(ui.pushButtonUp, &QPushButton::clicked, this, &CCorrectResult::slotUp);
	connect(ui.pushButtonDown, &QPushButton::clicked, this, &CCorrectResult::slotDown);
	connect(ui.widget, SIGNAL(sigShowImg(QString)), this, SLOT(slotShowImg(QString)));
}

void CCorrectResult::initData()
{
	ui.comboBoxAlarmType->addItem(A2T("报警"));
	ui.comboBoxAlarmType->addItem(A2T("未报警"));
	//设置两时间编辑栏
	QDateTime timeTo = QDateTime::currentDateTime();
	timeTo.setTime(QTime::fromString("23:59:59", "hh:mm:ss"));
	QDateTime timeFrom = QDateTime::currentDateTime();
	timeFrom.setTime(QTime::fromString("00:00:00", "hh:mm:ss"));
	ui.dateTimeEditBegin->setDateTime(timeFrom);
	ui.dateTimeEditEnd->setDateTime(timeTo);
	ui.dateTimeEditBegin->setCalendarPopup(true);
	ui.dateTimeEditEnd->setCalendarPopup(true);
	ui.widget->InitUi(4);
}

void CCorrectResult::slotPushButtonQuery()
{
	QVector<QSqlRecord> vecRecord;
	QString sErrMsg = "";
	QString sStartTime = ui.dateTimeEditBegin->dateTime().toString("yyyy/MM/dd hh:mm:ss");
	QString sEndTime = ui.dateTimeEditEnd->dateTime().toString("yyyy/MM/dd hh:mm:ss");
	int nAlarm = 0;
	if (ui.comboBoxAlarmType->currentText() == A2T("报警"))
	{
		nAlarm = 1;
	}
	else if (ui.comboBoxAlarmType->currentText() == A2T("未报警"))
	{
		nAlarm = 0;
	}
	CSqlOper::instance().selectCorrectResult(m_sPointGuid, vecRecord, sStartTime, sEndTime,nAlarm, sErrMsg);
	m_nTotalCount = vecRecord.size();
	if (m_nTotalCount % m_nPerPageCount == 0)
	{
		m_nPageCount = m_nTotalCount / m_nPerPageCount;
	}
	else
	{
		m_nPageCount = m_nTotalCount / m_nPerPageCount +1;
	}
	if (m_nCurrentPage > m_nPageCount)
	{
		m_nCurrentPage = 1;
	}
	ui.label->setText(A2T("%1/%2页").arg(m_nCurrentPage).arg(m_nPageCount));
	if (m_nCurrentPage == 1)
	{
		ui.pushButtonUp->setEnabled(false);
	}
	else
	{
		ui.pushButtonUp->setEnabled(true);
	}
	if (m_nCurrentPage == m_nPageCount)
	{
		ui.pushButtonDown->setEnabled(false);
	}
	else
	{
		ui.pushButtonDown->setEnabled(true);
	}
	//计算只展示的内容
	int nStart = (m_nCurrentPage - 1) * m_nPerPageCount;//第一页0-99 第二页100-199 共101条
	int nEnd = (m_nCurrentPage - 1) * m_nPerPageCount + m_nPerPageCount;
	if (nEnd > m_nTotalCount)
	{
		nEnd = m_nTotalCount;
	}
	QVector<QVector<QString>>vecStr;
	vecStr.clear();
	for (int i = nStart; i < nEnd; i++)
	{
		QVector<QString> vecRow;
		vecRow.push_back(QString::number(i));//序号
		vecRow.push_back(vecRecord[i].value("POINT_NAME").toString());//跑偏点名称
		if (vecRecord[i].value("RECTIFY_START_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss") == "")
		{
			vecRow.push_back("/");//跑偏开始时间
		}
		else
		{
			vecRow.push_back(vecRecord[i].value("RECTIFY_START_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss"));//跑偏开始时间
		}
		if (vecRecord[i].value("RECTIFY_END_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss") == "")
		{
			vecRow.push_back("/");//跑偏开始时间
		}
		else
		{
			vecRow.push_back(vecRecord[i].value("RECTIFY_END_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss"));//跑偏结束时间
		}
		vecRow.push_back(QString::number(vecRecord[i].value("LEFT_CURREENT_VALUE").toString().toDouble(), 'f',2));//左侧跑偏值
		vecRow.push_back(QString::number(vecRecord[i].value("RIGHT_CURREENT_VALUE").toString().toDouble(), 'f', 2));//右侧跑偏值
		vecRow.push_back(vecRecord[i].value("ALARM_DESC").toString());//跑偏报警描述
		vecRow.push_back(vecRecord[i].value("HTTP_PIC_URL").toString());//跑偏图
		vecStr.push_back(vecRow);
	}

	ui.widget->InitData(4, vecStr);
}

void CCorrectResult::slotPushButtonResult()
{
	CVideoResultQuery oper;
	oper.exec();
}

void CCorrectResult::slotDelDay()
{
	QDateTime timeTo = ui.dateTimeEditBegin->dateTime().addDays(-1);
	timeTo.setTime(QTime::fromString("23:59:59", "hh:mm:ss"));
	QDateTime timeFrom = ui.dateTimeEditBegin->dateTime().addDays(-1);
	timeFrom.setTime(QTime::fromString("00:00:00", "hh:mm:ss"));
	ui.dateTimeEditBegin->setDateTime(timeFrom);
	ui.dateTimeEditEnd->setDateTime(timeTo);
	slotPushButtonQuery();
}

void CCorrectResult::slotAddDay()
{
	QDateTime timeTo = ui.dateTimeEditBegin->dateTime().addDays(1);
	timeTo.setTime(QTime::fromString("23:59:59", "hh:mm:ss"));
	QDateTime timeFrom = ui.dateTimeEditBegin->dateTime().addDays(1);
	timeFrom.setTime(QTime::fromString("00:00:00", "hh:mm:ss"));
	ui.dateTimeEditBegin->setDateTime(timeFrom);
	ui.dateTimeEditEnd->setDateTime(timeTo);
	slotPushButtonQuery();
}

void CCorrectResult::slotUp()
{
	if (m_nCurrentPage > 1)
	{
		m_nCurrentPage--;
	}
	slotPushButtonQuery();
}

void CCorrectResult::slotDown()
{
	if (m_nCurrentPage < m_nPageCount)
	{
		m_nCurrentPage++;
	}
	slotPushButtonQuery();
}

void CCorrectResult::slotShowImg(QString sUrl)
{
	CImgShowDlg oper(sUrl);
	oper.exec();
}
